Implements outputSchema validation#566
Merged
alexhancock merged 3 commits intomodelcontextprotocol:mainfrom Dec 8, 2025
Merged
Implements outputSchema validation#566alexhancock merged 3 commits intomodelcontextprotocol:mainfrom
alexhancock merged 3 commits intomodelcontextprotocol:mainfrom
Conversation
95984d1 to
f30567b
Compare
Contributor
Author
|
Hi @jokemanfire, @4t145, @alexhancock, could one of you please review this PR? Thanks! 🙏 |
f30567b to
3c62ee8
Compare
3399b25 to
3283a0b
Compare
alexhancock
previously approved these changes
Dec 8, 2025
Contributor
alexhancock
left a comment
There was a problem hiding this comment.
Good catch. LGTM other than the one recommendation!
| } | ||
|
|
||
| /// Call [`schema_for_output`] with a cache. | ||
| pub fn cached_schema_for_output<T: JsonSchema + std::any::Any>() -> Result<Arc<JsonObject>, String> |
Contributor
There was a problem hiding this comment.
I think I would just fold this caching logic into schema_for_output and only have the one public method
Contributor
Author
There was a problem hiding this comment.
Thanks for the suggestion, @alexhancock! I've consolidated the caching logic into schema_for_output.
77177f2 to
17e48aa
Compare
alexhancock
approved these changes
Dec 8, 2025
Merged
|
Thank you for fixing this, much appreciated! |
Merged
daixijun
pushed a commit
to daixijun/mcp-rust-sdk
that referenced
this pull request
Dec 9, 2025
* feat: implement output schema validation * fix: calculator example comply MCP spec * refactor: merge cached_schema_for_output into schema_for_output
9 tasks
takumi-earth
pushed a commit
to earthlings-dev/rmcp
that referenced
this pull request
Jan 27, 2026
* feat: implement output schema validation * fix: calculator example comply MCP spec * refactor: merge cached_schema_for_output into schema_for_output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #532
Implements validation to enforce MCP specification requirement that tool
outputSchemamust have a root type of"object". Tools using structured output (Json<T>) where T is a primitive type will fail at compile time with clear error messages.Motivation and Context
As reported in issue #532, the MCP specification requires tool
outputSchemato have a root type of"object"(see MCP Tool Schema). The Rust SDK did not validate this, allowing spec-violating schemas to be generated.The latest spec published on Nov 25 states this more clearly:
How Has This Been Tested?
Noticed the
subtool in the calculator example in the repo doesn't comply with the MCP spec, which causes an issue with MCP Insepctor.rust-sdk/examples/servers/src/common/calculator.rs
Lines 46 to 49 in 94428d5
Now that the output schema validation is in place, the server panics during startup:
➜ servers git:(outputschema-validation) ✗ cargo run -p mcp-server-examples --example servers_calculator_stdio Compiling mcp-server-examples v0.1.5 (/Users/dale/work/rust-sdk/examples/servers) Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.31s Running `/Users/dale/work/rust-sdk/target/debug/examples/servers_calculator_stdio` 2025-11-29T18:50:44.901451Z INFO servers_calculator_stdio: Starting Calculator MCP server thread 'main' panicked at examples/servers/src/common/calculator.rs:46:5: Invalid output schema for Json<i32>: MCP specification requires tool outputSchema to have root type 'object', but found 'integer'. note: run with `RUST_BACKTRACE=1` environment variable to display a backtraceAfter fixing the calculator example code so that the
subtool returns unstructured output just like thesumtool, the server starts up without any issues and works well with the MCP Inspector.➜ servers git:(outputschema-validation) ✗ cargo run -p mcp-server-examples --example servers_calculator_stdio Compiling mcp-server-examples v0.1.5 (/Users/dale/work/rust-sdk/examples/servers) Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.41s Running `/Users/dale/work/rust-sdk/target/debug/examples/servers_calculator_stdio` 2025-11-29T19:05:00.312995Z INFO servers_calculator_stdio: Starting Calculator MCP serverAs suggested in issue #532, wrapping the primitive type also works as expected.
Breaking Changes
Existing valid code works unchanged:
i32,String) with no output stream continues to work<Json<i32>,<Json<String>) fails to compileTypes of changes
Checklist
Additional context
The TypeScript SDK also enforces this requirement in the Tool schema definition here: